//
//  TTSetting.m
//  Tint
//
//  Created by Kevin Wu on 12/7/12.
//
//

#import "TTSetting.h"

@implementation TTSetting


TTSynthesizeSingleton(TTSetting);

#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        NSString *path = [TTDocumentsPath() stringByAppendingPathComponent:@"AppSettings.xml"];
        _settings = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
        if ( _settings == nil ) {
            _settings = [[NSMutableDictionary alloc] init];
        }
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_settings);
    [super dealloc];
}


#pragma mark - Public

- (NSArray *)keys
{
    return [_settings allKeys];
}

- (NSUInteger)count
{
    return [_settings count];
}

- (BOOL)hasValueForKey:(NSString *)key
{
    id object = [_settings objectForKey:key];
    return ( object != nil );
}


- (BOOL)boolForKey:(NSString *)key
{
    return [[_settings objectForKey:key] boolValue];
}

- (int)intForKey:(NSString *)key
{
    return [[_settings objectForKey:key] intValue];
}

- (long long)longLongForKey:(NSString *)key
{
    return [[_settings objectForKey:key] longLongValue];
}

- (double)doubleForKey:(NSString *)key
{
    return [[_settings objectForKey:key] doubleValue];
}

- (NSString *)stringForKey:(NSString *)key
{
    return [_settings objectForKey:key];
}

- (id)objectForKey:(NSString *)key
{
    return [_settings objectForKey:key];
}


- (void)setBool:(BOOL)value forKey:(NSString *)key
{
    NSNumber *object = [[NSNumber alloc] initWithBool:value];
    [self setObject:object forKey:key];
    TTRelease(object);
}

- (void)setInt:(int)value forKey:(NSString *)key
{
    NSNumber *object = [[NSNumber alloc] initWithInt:value];
    [self setObject:object forKey:key];
    TTRelease(object);
}

- (void)setLongLong:(long long)value forKey:(NSString *)key
{
    NSNumber *object = [[NSNumber alloc] initWithLongLong:value];
    [self setObject:object forKey:key];
    TTRelease(object);
}

- (void)setDouble:(double)value forKey:(NSString *)key
{
    NSNumber *object = [[NSNumber alloc] initWithDouble:value];
    [self setObject:object forKey:key];
    TTRelease(object);
}

- (void)setString:(NSString *)value forKey:(NSString *)key
{
    [self setObject:value forKey:key];
}

- (void)setObject:(NSObject *)value forKey:(NSString *)key
{
    if ( value ) {
        [_settings setObject:value forKeyIfNotNil:key];
    } else {
        [_settings removeObjectForKeyIfNotNil:key];
    }
}


- (BOOL)synchronize
{
    NSString *path = [TTDocumentsPath() stringByAppendingPathComponent:@"AppSettings.xml"];
    return [_settings writeToFile:path atomically:YES];
}

- (void)dump
{
#ifdef DEBUG
    NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    NSLog(@"+");
    NSLog(@"+ total: %d", [_settings count]);
    NSLog(@"+");
    NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    for ( NSString *key in [_settings keyEnumerator] ) {
        NSLog(@"+ %@ = %@", key, [_settings objectForKey:key]);
        NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    }
#endif
}

@end
